package com.example.xmhds_1.mapper.userinfo;

import com.example.xmhds_1.entity.UserInfo;
import org.apache.ibatis.annotations.*;

/**
 * 用户信息数据访问接口
 */
@Mapper
public interface UserInfoMapper {

    /**
     * 根据账号ID查询用户信息
     * 只返回未删除的记录
     *
     * @param accountId 账号ID
     * @return 用户信息，不存在返回null
     */
    @Select("SELECT * FROM user_info WHERE account_id = #{accountId} AND deleted = 0")
    UserInfo selectByAccountId(@Param("accountId") String accountId);

    /**
     * 插入新的用户信息
     * 会自动设置id
     *
     * @param userInfo 用户信息
     * @return 影响的行数
     */
    @Insert("INSERT INTO user_info (account_id, username, create_time, update_time, deleted) " +
            "VALUES (#{accountId}, #{username}, #{createTime}, #{updateTime}, #{deleted})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserInfo userInfo);

    /**
     * 更新用户信息
     * 可更新用户名和更新时间
     *
     * @param userInfo 用户信息
     * @return 影响的行数
     */
    @Update("UPDATE user_info SET " +
            "username = #{username}, " +
            "update_time = #{updateTime} " +
            "WHERE id = #{id} " +
            "AND deleted = 0")
    int updateById(UserInfo userInfo);

    /**
     * 逻辑删除用户信息
     *
     * @param id 用户信息ID
     * @return 影响的行数
     */
    @Update("UPDATE user_info SET " +
            "deleted = 1, " +
            "update_time = NOW() " +
            "WHERE id = #{id} " +
            "AND deleted = 0")
    int deleteById(@Param("id") Long id);

    /**
     * 通过accountId查询用户信息
     * @param accountId
     * @return
     */
    @Select("SELECT * FROM user_info WHERE account_id = #{accountId} AND deleted = 0")
    UserInfo selectOne(String accountId);
    /*
    * 通过账号获取对应的accountId
    * */
    @Select("SELECT account_id FROM user_info WHERE username = #{username} AND deleted = 0")
    String getUserIdByUsername(String username);
}
